﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.Xml.Serialization;

namespace Extensil.Common.CategoryHandler
{
    public class PluginHandler :ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return this["name"] as string;
            }
            set
            {
                this["name"] = value;
            }
        }
        [ConfigurationProperty("path", IsRequired = true)]
        public string Path
        {
            get
            {
                return this["path"] as string;
            }
            set
            {
                this["path"] = value;
            }
        }
        [ConfigurationProperty("order", IsRequired = false)]
        public int Order
        {
            get
            {
                try
                {
                    int order = (int)this["order"];
                    if(order == 0)
                        return -1;
                    return order;
                }
                catch(Exception exc)
                {
                    return -1;
                }
            }
            set
            {
                this["order"] = value;
            }
        }
        [ConfigurationProperty("plugName", IsRequired = true)]
        public string PlugName
        {
            get
            {
                return this["plugName"] as string;
            }
            set
            {
                this["plugName"] = value;
            }
        }
        [ConfigurationProperty("version", IsRequired = false)]
        public string Version
        {
            get
            {
                return this["version"] as string;
            }
            set
            {
                this["version"] = value;
            }
        }
        [ConfigurationProperty("hidden", IsRequired = false)]
        public bool Hidden
        {
            get
            {
                bool hidden = false;
                bool.TryParse(this["hidden"].ToString(), out hidden);
                return hidden;
            }
            set
            {
                this["hidden"] = value;
            }
        }
        [ConfigurationProperty("autostart", IsRequired = false)]
        public bool AutoStart
        {
            get
            {
                bool visible = false;
                bool.TryParse(this["autostart"].ToString(), out visible);
                return visible;
            }
            set
            {
                this["autostart"] = value;
            }
        }
        [ConfigurationProperty("hotKey", IsRequired = false)]
        public string HotKey
        {
            get
            {
                return this["hotKey"].ToString() as String;
            }
            set
            {
                this["hotKey"] = value;
            }
        }

        // [XmlArray("appSettings"), XmlArrayItem(typeof(CategorySettings), ElementName = "add")]
        [ConfigurationProperty("appSettings")]
        [ConfigurationCollection(typeof(CategorySettings), AddItemName = "add")]
        public CategorySettings CategorySettings
        {
            get
            {
                return this["appSettings"] as CategorySettings;
            }
            set
            {
                this["appSettings"] = value;
            }
        }

        public override bool IsReadOnly()
        {
            return false;
        }

        public bool HasLinkedHandler
        {
            get
            {
                string[] catClass = Path.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                string handler = catClass[1].Trim();
                foreach(PluginHandler cat in CategorySection.GetConfig().CategoryHandlers)
                {
                    if(cat.Name == this.Name)
                        continue;
                    string[] cClass = cat.Path.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    string hnd = cClass[1].Trim();
                    if(hnd.ToLower() == handler.ToLower())
                        return true;
                }
                return false;
            }
        }
    }
    
}
